import unittest
import requests
from appium import webdriver
import time
import HTMLTestReportCN
import warnings

class AppUI(unittest.TestCase):


    def setUp(self):
        warnings.simplefilter("ignore",ResourceWarning)#忽略警告
        # -----------------------------包名相关信息-----------------
        desired_caps = {'platformName': 'Android',
                             'deviceName': '127.0.0.1:7555',
                             'platformVersion': '8.0',
                             'appPackage': 'com.tuya.uat',
                             'appActivity': 'com.ssf.tckotlin.ui.activity.welcome.WelcomeActivity'
                             }
        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

        print('start App')
        # -----------------------------------------------------------
    # ---------------------------定义函数------------------------
    # 函数isElement，作用：判断页面元素是否存在，如果页面存在元素xx，则进行操作，不存在进行下一步
    def isElement(self,id):
        try:
            self.driver.find_element_by_id(id)
            stateOne = True
        except:
            stateOne = False
        if stateOne == True:
            self.driver.find_element_by_id(id).click()

    # 函数swipeToUp，作用：滑动，向上滑动
    def swipeToUp(self,during):
        width = self.driver.get_window_size()['width']
        height = self.driver.get_window_size()['height']
        self.driver.swipe(width / 2, height * 3 / 4, width / 2, height / 4, during)

    # 函数swipeTodown，作用：滑动，向下滑动
    def swipeToDown(self,during):
        width = self.driver.get_window_size()['width']
        height = self.driver.get_window_size()['height']
        self.driver.swipe(width / 2, height / 4, width / 2, height * 3 / 4, during)
    # -----------------------------------------------------------
    def test_login(self):
        # 判断是否有公告
        self.isElement('btn_cancel')
        print('如果有公告，点击公告')
        # 判断是否已经登录
        try:
            self.driver.find_element_by_xpath('//android.widget.TextView[contains(@text, "我的")]')
            stateTwo = True
        except:
            stateTwo = False
        print('如果已经登录，推出重新登录')
        # stateTwo 为真，说明已经登录，直接进入直播间
        # if stateTwo == True:
        #     self.driver.find_element_by_xpath('//android.widget.TextView[contains(@text, "我的")]').click()
        #     # 退出登录
        #     # 滑动屏幕到底部
        #     self.driver.implicitly_wait(3)
        #     self.swipeToUp(200)
        #     self.swipeToUp(200)
        #     self.driver.find_element_by_id('ll_setting').click()
        #     self.driver.find_element_by_id('setting_out').click()
        #     self.driver.find_element_by_id('dialog_tv_ensure').click()

        #stateTwo 为假，说明没有登录，需要进行登录操作
        #登录
        #判断输入框中是否存在文字，如果存在需要点击清除按钮
        # print('输入账号密码')
        # self.isElement('tv_user_name')
        # self.isElement('log_username_iv')
        # self.driver.find_element_by_id('log_username_et').click()
        # self.driver.find_element_by_id('log_username_et').clear()
        # self.driver.find_element_by_id('log_username_et').send_keys('13333333333')
        # self.driver.find_element_by_id('log_psd_et').click()
        # self.driver.find_element_by_id('log_psd_et').clear()
        # self.driver.find_element_by_id('log_psd_et').send_keys('111111')
        # self.driver.find_element_by_id('log_log_btn').click()

        # # 判断是否有公告
        # self.driver.implicitly_wait(5)
        # self.isElement('btn_cancel')
        # # 进入直播模块
        # try:
        #     self.driver.find_element_by_xpath('//android.widget.TextView[contains(@text, "直播")]').click()
        # except:
        #     stateTwo = False
        # # 点击直播模式，进入直播列表，进入直播房间
        # print('兔牙直播')
        # self.driver.find_element_by_id('iv_buy_icon').click()
        # self.driver.find_element_by_id('rb_live_mode').click()
        # print('进入房间')
        # self.driver.find_element_by_id('iv_item_buylott_live_cover').click()
        #
        # # 页面是否存在新手教程，存在跳过新手教程
        # self.isElement('iv_skip')
        # self.driver.implicitly_wait(3)
        # self.driver.find_element_by_id('history_btn').click()
        # self.driver.find_element_by_xpath('//android.widget.TextView[contains(@text, "投注记录")]').click()
        # self.driver.find_element_by_xpath('//android.widget.TextView[contains(@text, "开奖记录")]').click()
        # self.driver.find_element_by_id('iv_close').click()



    def tearDown(self):
        #print('接口测试通过')
        pass

if __name__ == "__main__":
    unittest.main()